package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shop/api/request"
	"shop/common/global"
	"shop/user_grpc/proto/user"
)

func Register(c *gin.Context) {

	var dev request.UserReg
	if err := c.ShouldBind(&dev); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	register, err := global.UserClient.Register(c, &user.RegisterRequest{
		Account:  dev.Account,
		Password: dev.Password,
		Code:     dev.Code,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "注册成功",
		"data": register,
	})
}

func Login(c *gin.Context) {
	var dev request.UserReg
	if err := c.ShouldBind(&dev); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	login, err := global.UserClient.Login(c, &user.LoginRequest{
		Account:  dev.Account,
		Password: dev.Password,
		Code:     dev.Code,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "登录成功",
		"data": login,
	})
}

func FindMe(c *gin.Context) {
	var dev request.UserReg
	if err := c.ShouldBind(&dev); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	me, err := global.UserClient.FindMe(c, &user.FindMeRequest{
		Account:  dev.Account,
		Password: dev.Password,
		Code:     dev.Code,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "密码找回成功",
		"data": me,
	})
}

func Address(c *gin.Context) {
	var dev request.Address
	if err := c.ShouldBind(&dev); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	_, err := global.UserClient.CreateAddress(c, &user.CreateAddressRequest{
		UserId: int32(dev.UserId),
		Phone:  dev.Phone,
		Area:   dev.Area,
		Status: int32(dev.Status),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "success",
	})
}

func Coll(c *gin.Context) {
	var dev request.AddColl
	if err := c.ShouldBind(&dev); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	_, err := global.UserClient.UserColl(c, &user.UserCollRequest{
		UserId: int32(dev.UserId),
		GoodId: int32(dev.GoodsId),
	})
	
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "success",
	})
}
